package com.globalwave.customer.service;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.globalwave.base.BaseServiceImpl;
import com.globalwave.common.ArrayOrderList;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.exception.BusinessException;
import com.globalwave.customer.entity.CustomerProfile;
import com.globalwave.customer.entity.CustomerProfileSO;


@Service("customerProfileBO")
@Scope("prototype")
@Transactional
public class CustomerProfileBO extends BaseServiceImpl {


    public CustomerProfile get(Long cid) throws Exception {
    	
    	if (cid == null) {
    		throw new BusinessException("101000") ;
    	}
    	
    	CustomerProfile criterion = new CustomerProfile() ;
        criterion.setCid(cid) ;
        
        CustomerProfile result = (CustomerProfile)jdbcDao.find(criterion) ;

        
        if (result == null) {
        	throw new BusinessException("101001") ;
        }
        
        return result ;
    } 

    public CustomerProfile create(CustomerProfile profile) throws Exception {
    	checkCid(profile) ;
        return (CustomerProfile) jdbcDao.insert(profile);
    }

    public void update(CustomerProfile profile) throws Exception {
    	CustomerProfile old = new CustomerProfile() ;
    	old.setId(profile.getId()) ;    	
    	
    	old = (CustomerProfile)jdbcDao.find(old) ;
    	
    	if(!old.getCid().equals(profile.getCid())) {
    		checkCid(profile) ;
    	}
        jdbcDao.update(profile) ;
    }

    public void delete(CustomerProfile profile) throws Exception {
        // delete cascade 
        //OrganizationProfile orgProfile = new OrganizationProfile() ;
        //orgProfile.setProfile_id(profile.getProfile_id()) ;
        //jdbcDao.delete(OrganizationProfile.class, orgProfile) ;
        
        jdbcDao.delete(profile) ;
        
    }

    public void deleteAll(Long[] ids) throws Exception {
        CustomerProfileSO criterion = new CustomerProfileSO() ;
        criterion.setIds(ids) ;
        jdbcDao.delete(CustomerProfile.class, criterion) ;
        
    }
    
    private void checkCid(CustomerProfile profile) throws Exception {

    	if (profile.getCid() == null) {
    		profile.setCid(nextCid()) ;
    	} else {
    		if (existCid(profile) > 0) {
    			throw new BusinessException("101002") ;//cid.is.exist-顾客编号已经被使用，请重新输入！
    		}
    	}
    }
    
    private long nextCid() throws Exception {
    	return this.jdbcDao.getName("customerSQLs:nextCid", null, new Long(1)) ;
    }
    
    private long existCid(CustomerProfile p) throws Exception {
    	return this.jdbcDao.getName("customerSQLs:existCid", p, new Long(0)) ;
    }
    
    public ArrayPageList<CustomerProfile> query(
            CustomerProfileSO criterion, 
            int pageIndex, 
            int pageSize, 
            ArrayOrderList orderList) throws Exception {
        
        if (orderList == null) {
            orderList = new ArrayOrderList() ;
            //orderList.addAsc("profile_id") ;
        }
        
        return (ArrayPageList<CustomerProfile>)jdbcDao.query(criterion, pageIndex, pageSize, orderList, CustomerProfile.class);
    }
}
